package com.supor.aiot.home.apdater;


import android.graphics.Typeface;

import com.supor.aiot.common.adapter.BaseBindingAdapter;
import com.supor.aiot.common.adapter.BaseBindingHolder;
import com.supor.aiot.common.entity.ProductType;
import com.supor.aiot.home.R;
import com.supor.aiot.home.databinding.ItemNavigationBinding;

public class NavigationAdapter extends BaseBindingAdapter<ProductType, ItemNavigationBinding> {

    public NavigationAdapter() {
        super(R.layout.item_navigation);
    }

    @Override
    protected void bindView(BaseBindingHolder holder, ProductType dataBean,
                            ItemNavigationBinding binding, int position) {
        if (dataBean != null) {
            String name = dataBean.modelTypeName;
            if (name != null) {
                StringBuilder name5 = new StringBuilder();
                while (name.length() > 5) {
                    name5.append(name.substring(0, 5));
                    name5.append("\n");
                    name = name.substring(5);
                }
                if (name.length() == 0) {
                    if ("\n".equalsIgnoreCase(String.valueOf(name5.charAt(name5.length() - 1)))) {
                        name5.substring(0, name5.length() - 1);
                    }
                } else {
                    name5.append(name);
                }
                binding.tvTitle.setText(name5);
            }
            // binding.tvTitle.setText(dataBean.modelTypeName);
            binding.tvTitle.setSelected(dataBean.selected);
            binding.setBean(dataBean);
            if (dataBean.selected) {
                binding.tvTitle.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                binding.tvTitle.setTypeface(Typeface.DEFAULT);
            }
            binding.tvTitle.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onSelected(position);
                }
            });
        }
    }

    private OnSelectListener listener;

    public void setOnSelectListener(OnSelectListener listener) {
        this.listener = listener;
    }

    public interface OnSelectListener {
        void onSelected(int position);
    }

}
